(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var currentPage = 1;
	var pageSize = 10;
	var totalSize;
	var now = 0;
	var dataCache = [];
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			initGradeClass();
			pagebtns();
		});
	});
	
	function initGradeClass(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var data = {
				cmd:"getDataRole",
				model:"license",
				userKey:userKey,
				data:{
					rolecode:userRoles
				},
				remark:"",
		};
		AjaxCrud.get2(serverUrl.ServerLoginUrl,data,function(res){
			var typeOpt = "<option value='' >学段</option>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data;
				classCaches = data;
				for(var i = 0; i < data.length; i++){
					var name = "";
					if(data[i].type == "0"){
						name = "幼儿园";
					}else if(data[i].type == "1"){
						name = "小学";
					}else if(data[i].type == "2"){
						name = "中学";
					}else if(data[i].type == "3"){
						name = "高中";
					}
					typeOpt += "<option value='"+data[i].type+"'>"+name+"</option>";
				}
				$("#type-aterminal").html(typeOpt);
				
				$("#type-aterminal").unbind("change");
				$("#type-aterminal").change(function(){
					var gradeData = null;
					var type = $("#type-aterminal option:selected").val();
					var gradeOpt = "<option value='' classcodes='' classes=''>年级</option>";
					if(type != ""){
						for(var i = 0; i < data.length; i++){
							if(data[i].type == type){
								gradeData = data[i].data;
							}
						}
						for(var i = 0; i < gradeData.length; i++){
							gradeOpt += "<option value='"+gradeData[i].grade+"' classcodes='"+gradeData[i].classcode+"' classes='"+gradeData[i].classes+"'>"+gradeData[i].grade+"年级</option>";
						}
					}
					$("#grade-aterminal").html(gradeOpt);
					
					var classOpt = "<option value='' classes=''>班级</option>";
					$("#class-aterminal").html(classOpt);
					
					initTerminals(1,pageSize);
				});
				
				$("#grade-aterminal").unbind("change");
				$("#grade-aterminal").change(function(){
					var classes = $("#grade-aterminal option:selected").attr("classes").split(",");
					var classcodes = $("#grade-aterminal option:selected").attr("classcodes").split(",");
					var classOpt = "";
					for(var j = 0; j < classes.length; j++){
						if(classcodes[j] == "" && classes[j] == ""){
							classOpt += "<option value='' classes=''>班级</option>";
						}else{
							classOpt += "<option value='"+classcodes[j]+"' classes='"+classes[j]+"'>"+classes[j]+"班</option>";
						}
					}
					$("#class-aterminal").html(classOpt);
					
					initTerminals(1,pageSize);
				});
				$("#class-aterminal").unbind("change");
				$("#class-aterminal").change(function(){
					initTerminals(1,pageSize);
				});
			}else{
				
			}
			initTerminals(1,pageSize);
		},null,null);
	}
	
	function initTerminals(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var userRoles = $("#user-name",parent.parent.document).attr("rolecodes");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $("#class-aterminal option:selected").val();
		var type = $("#type-aterminal option:selected").val();
		var studenteName = $("#aterminal-name").val();
		var imei = $("#aterminal-imei").val();
		if(classcode == undefined){
			classcode = "";
		}
		var data = {
				cmd:"getCards",
				model:"card",
				userKey:userKey,
				data:{
					rolecode:userRoles,
					classcode:classcode,
					studentname:encodeURI(studenteName),
					imei:imei,
					currentpage:currentpage,
					pagesize:pagesize,
					schoolcode:schoolcode,
					schooltype:type
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.list;
				var total = res.data.total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#aterminal-page").empty();
				$("#aterminal-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					dataCache = [];
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initTerminals(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initTerminals((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize%5 == 0){
							if((now+1) < totalSize/5){
								now ++;
								initTerminals((now*5+1),pageSize);
							}else{
								return false;
							}
						}else{
							if((now+1) <= (totalSize-totalSize%5)/5){
								now ++;
								initTerminals((now*5+1),pageSize);
							}else{
								return false;
							}
						}
					}
				});
				
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" ){
								data[i][key] = "";
							}
						}
//						var lockBtns = "";
//						if(data[i].islock == true || data[i].islock == "true"){
//							lockBtns = "<div class=\"table-locked\" usercode = '"+data[i].usercode+"' id=\"lockuser\" title=\"锁定\"></div>";
//						}else if(data[i].islock == false || data[i].islock == "false"){
//							lockBtns = "<div class=\"table-unlocked\" usercode = '"+data[i].usercode+"' id=\"unlockuser\" title=\"解锁\"></div>";
//						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-headimg\">" +
								"<img src=\"../../../../../../"+ServiceName+data[i].headimg+"\" width=\"100%\" height=\"100%\"/>" +
							"</div>" +
							"<div class=\"tables-left\">" +
								"<div class=\"table-name\" title=\""+data[i].studentname+"\"\>"+substr(data[i].studentname,6)+"</div>" +
								"<div class=\"table-code\" style=\"width:140px;\" title=\"IMEI\">"+data[i].imei+"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
							"<div class=\"table-name\" title=\"\">"+data[i].gradeclass+"</div>" +
								"<div class=\"table-class\" title=\"手机号\">"+data[i].cellphone+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-address\" title=\""+data[i].remark+"\" >"+substr(data[i].remark,12)+"</div>" +
								"<div class=\"table-oprate\">" +
									"<div class=\"table-detail\" imei = \""+data[i].imei+"\" studentcode = \""+data[i].studentcode+"\" title=\"详情\"></div>" +
									"<div class=\"table-edit\" imei = \""+data[i].imei+"\" studentcode = \""+data[i].studentcode+"\" title=\"设置\"></div>" +
									"<div class=\"table-locked\" imei = \""+data[i].imei+"\"  title=\"挂失\"></div>" +
									//lockBtns +
								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#aterminal-data-show").empty();
					$("#aterminal-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					
					$(".table-detail").unbind("click");
					$(".table-detail").click(function(){
						$("#aterminal-detailform").show();
						$("#right-aterminal-tables").hide();
						var imei = parseInt($(this).attr("imei"));
						showData(imei);
						$("#aterminal-detailform").contents().find(".add-stu-bottom1").hide();
						$("#aterminal-detailform").contents().find("input").attr("disabled","disabled");
						$("#aterminal-detailform").contents().find("input[type='text']").css("background-color","#E5E5E5");
						$("#aterminal-detailform").contents().find(".add-stu-bottom1").css("display","");
						$("#aterminal-detailform").contents().find(".save-btns").css("display","none");
						$("#aterminal-detailform").contents().find("#back-to-table").unbind("click");
						$("#aterminal-detailform").contents().find("#back-to-table").click(function(){
							$("#aterminal-detailform").hide();
							$("#right-aterminal-tables").show();
						});
					});
					$(".table-edit").unbind("click");
					$(".table-edit").click(function(){
						$("#aterminal-detailform").show();
						$("#right-aterminal-tables").hide();
						var imei = parseInt($(this).attr("imei"));
						var studentcode = parseInt($(this).attr("studentcode"));
						showData(imei);
						$("#aterminal-detailform").contents().find(".add-stu-bottom1").show();
						$("#aterminal-detailform").contents().find("input").removeAttr("disabled","disabled");
						$("#aterminal-detailform").contents().find("input").css("background-color","#FFF");
						$("#aterminal-detailform").contents().find(".save-btns").css("display","");
						
						$("#aterminal-detailform").contents().find("#back-to-table").unbind("click");
						$("#aterminal-detailform").contents().find("#back-to-table").click(function(){
							$("#aterminal-detailform").hide();
							$("#right-aterminal-tables").show();
						});
						$("#aterminal-detailform").contents().find("#white-list-set").unbind("click");
						$("#aterminal-detailform").contents().find("#white-list-set").click(function(){
							if(check2("white","aterminal-detailform")){
								addWhiteList(imei,studentcode);
							}
						});
						$("#aterminal-detailform").contents().find("#parent-list-set").unbind("click");
						$("#aterminal-detailform").contents().find("#parent-list-set").click(function(){
							if(check2("parent","aterminal-detailform")){
								addParentList(imei);
							}
						});
						$("#aterminal-detailform").contents().find("#sostype-set").unbind("click");
						$("#aterminal-detailform").contents().find("#sostype-set").click(function(){
							addSosType(imei);
						});
						$("#aterminal-detailform").contents().find("#sos-list-set").unbind("click");
						$("#aterminal-detailform").contents().find("#sos-list-set").click(function(){
							if(check2("sos","aterminal-detailform")){
								addSosList(imei);
							}
						});
						$("#aterminal-detailform").contents().find("#worktime-list-set").unbind("click");
						$("#aterminal-detailform").contents().find("#worktime-list-set").click(function(){
							addWorktimelist(imei,1);
						});
						$("#aterminal-detailform").contents().find("#stealthtime-list-set").unbind("click");
						$("#aterminal-detailform").contents().find("#stealthtime-list-set").click(function(){
							addWorktimelist(imei,0);
						});
						$("#aterminal-detailform").contents().find("#terminalsetup-add").unbind("click");
						$("#aterminal-detailform").contents().find("#terminalsetup-add").click(function(){
							addSelectedRing(imei);
						});
						
						$("#aterminal-detailform").contents().find("#caller-set").unbind("click");
						$("#aterminal-detailform").contents().find("#caller-set").click(function(){
							var model = $("#aterminal-detailform").contents().find("input[name='card-model']:checked").val();
							addCaller(imei,model);
						});
						
						var sostype1 = $("#aterminal-detailform").contents().find("input[name='sostype1']:checked").val();
						if(sostype1 == 4){
							$("#aterminal-detailform").contents().find("input[name='sostype2']").attr("disabled","disabled");
							//$("#aterminal-detailform").contents().find("input[name='sostype2']").removeAttr("checked");
						}else if(sostype1 == 5){
							$("#aterminal-detailform").contents().find("input[name='sostype2']").removeAttr("disabled");
						}
						$("#aterminal-detailform").contents().find("#sostype-open").unbind("change");
						$("#aterminal-detailform").contents().find("#sostype-open").change(function(){
							$("#aterminal-detailform").contents().find("input[name='sostype2']").removeAttr("disabled");
							
						});
						$("#aterminal-detailform").contents().find("#sostype-close").unbind("change");
						$("#aterminal-detailform").contents().find("#sostype-close").change(function(){
							$("#aterminal-detailform").contents().find("input[name='sostype2']").attr("disabled","disabled");
							//$("#aterminal-detailform").contents().find("input[name='sostype2']").removeAttr("checked");
						});
						//验证
						var num = $("#aterminal-detailform").contents().find("input[name='aterminal-number']");
						for(var i = 0; i < num.length; i++){
							var id = num[i].getAttribute("id");
							check1(id,checkMobilePhone,"check","aterminal-detailform");
						}
					});
					$(".table-locked").unbind("click");
					$(".table-locked").click(function(){
						var imei = parseInt($(this).attr("imei"));
						deleteBox("确定要挂失吗？",function(){
							terminalLoss(imei);
						});
					});
					
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#aterminal-data-show").empty();
					$("#aterminal-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#aterminal-data-show").empty();
				$("#aterminal-data-show").append(content);
			}
		},null,null);	
	}
	function addCaller(imei,model){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"updateCard",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					model:model,
					schoolcode:schoolcode
				},
				remark:"",
		};
		processBegin("正在应用中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("设置成功");
			}else{
				processError("设置失败");
			}
		},null,null);
	}
	function showData(imei){
		getWhiteList(imei);
		getFamilyNumbers(imei);
		getSosNumbers(imei);
		getWorktimes(imei,1);
		getWorktimes(imei,0);
		getAllRings(imei);
	}
	
	function getWhiteList(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getPhonebooks",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null" ){
							list[i][key] = "";
						}
					}
					if($("#aterminal-detailform").contents().find("#white-number-" + (i+1)) && $("#aterminal-detailform").contents().find("#white-name-" + (i+1))){
						$("#aterminal-detailform").contents().find("#white-number-" + (i+1)).val(list[i].incellphone);
						$("#aterminal-detailform").contents().find("#white-name-" + (i+1)).val(list[i].remark);
					}
				}
			}
		},null,null);	
	}
	function getFamilyNumbers(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getFamilyphones",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null" ){
							list[i][key] = "";
						}
					}
					if($("#aterminal-detailform").contents().find("#parent-number-" + (i+1))){
						$("#aterminal-detailform").contents().find("#parent-number-" + (i+1)).val(list[i].familyphone);
						$("#aterminal-detailform").contents().find("#parent-name-" + (i+1)).val(list[i].remark);
					}
				}
			}
		},null,null);	
	}
	function getSosNumbers(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getSosphones",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				for(var i = 0; i < list.length; i++){
					for(var key in list[i]){
						if(list[i][key] == null || list[i][key] == "null" ){
							list[i][key] = "";
						}
					}
					if($("#aterminal-detailform").contents().find("#sos-number-" + (i+1))){
						$("#aterminal-detailform").contents().find("#sos-number-" + (i+1)).val(list[i].sosphone);
						$("#aterminal-detailform").contents().find("#sos-name-" + (i+1)).val(list[i].remark);
					}
				}
			}
		},null,null);	
	}
	
	function getWorktimes(imei,work){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getCardWork",
				model:"card",
				userKey:userKey,
				data:{
					cardcode:imei,
					work:work,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				if(work == 1){
					for(var i = 1; i < 25; i++){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null" ){
								list[i][key] = "";
							}
						}
						if($("#aterminal-detailform").contents().find("#worktime" + i + "-1") && $("#aterminal-detailform").contents().find("#worktime" + i + "-2")){
							$("#aterminal-detailform").contents().find("#worktime" + i + "-1").val(list["start" + i ]);
							$("#aterminal-detailform").contents().find("#worktime" + i + "-2").val(list["stop" + i ]);
						}
					}
				}else if(work == 0){
					for(var i = 1; i < 25; i++){
						for(var key in list[i]){
							if(list[i][key] == null || list[i][key] == "null" ){
								list[i][key] = "";
							}
						}
						if($("#aterminal-detailform").contents().find("#stealthtime" + i + "-1") && $("#aterminal-detailform").contents().find("#stealthtime" + i + "-2")){
							$("#aterminal-detailform").contents().find("#stealthtime" + i + "-1").val(list["start"+ i ]);
							$("#aterminal-detailform").contents().find("#stealthtime" + i + "-2").val(list["stop"+ i ]);
						}
					}
				}
			}
		},null,null);	
		
	}
	function getAllRings(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getAllRings",
				model:"card",
				userKey:userKey,
				data:{
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				var content = "";
				for(var i = 0; i < list.length; i = i+2){
					content += "<tr><td style='width:50%;'><input type=\"radio\" name=\"terminalsetup_name\" value='"+list[i].ring+"' ringurl='"+list[i].ringurl+"'/><a href='"+Host+list[i].ringurl+"' target=\"view_window\" class=\"rings\">铃声"+Number(list[i].ring)+"</a></td>";
					content += "<td style='width:50%;'><input type=\"radio\" name=\"terminalsetup_name\" value='"+list[i+1].ring+"' ringurl='"+list[i].ringurl+"' /><a href='"+Host+list[i+1].ringurl+"' target=\"view_window\" class=\"rings\">铃声"+Number(list[i+1].ring)+"</a></td></tr>";
				}
				$("#aterminal-detailform").contents().find("#table-ring").empty();
				$("#aterminal-detailform").contents().find("#table-ring").append(content);
				getSelectedRing(imei);
				
			}
		},null,null);	
	}
	function getSelectedRing(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getCard",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data;
				//显示已有的铃声设置
				var m = $("#aterminal-detailform").contents().find("#table-ring input");
				for(var i = 0; i < m.length; i++){	
					if(m[i].getAttribute("value") == list[0].ring)
					$("#aterminal-detailform").contents().find("#table-ring input[value="+list[0].ring+"]").attr("checked","checked");
				}
				//显示已有的报警类型设置
				var sostype2 = $("#aterminal-detailform").contents().find("input[name='sostype2']");
				var sostype = list[0].sostype;
				if(sostype == 4 || sostype == "4"){
					$("#aterminal-detailform").contents().find("#sostype-close").attr("checked","checked");
					sostype2.removeAttr("checked");
				}else if(sostype != "" && sostype != "null"){
					$("#aterminal-detailform").contents().find("#sostype-open").attr("checked","checked");
					for(var i = 0; i < sostype2.length; i++){
						if(sostype2[i].getAttribute("value") == sostype)
							$("#aterminal-detailform").contents().find("#sostype-" + sostype).attr("checked","checked");
					}
				}
				for(var i = 0; i < list.length; i++){
					$("#aterminal-detailform").contents().find("#card-model-"+list[i].model).attr("checked","checked");
				}
			}
		},null,null);	
	}
	
	function addParentList(imei){
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var list = [];
		for(var i = 0; i < 3; i ++){
//			if($("#aterminal-detailform").contents().find("#parent-number-" + (i+1)).val() != "" || $("#aterminal-detailform").contents().find("#parent-name-" + (i+1)).val() != ""){
				var obj = {};
				obj.imei = imei;
				obj.familyphone = $("#aterminal-detailform").contents().find("#parent-number-" + (i+1)).val();
				obj.remark = encodeURI($("#aterminal-detailform").contents().find("#parent-name-" + (i+1)).val());
				obj.schoolcode = schoolcode;
				for(var key in obj){
					if(obj[key] == undefined){
						obj[key] = "";
					}
				}
				list.push(obj);
//			}
		}
		if(list.length > 0){
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
					cmd:"addFamilyphones",
					model:"card",
					userKey:userKey,
					data:JSON.stringify(list),
					remark:"",
			};
			processBegin("正在应用中···");
			AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
				if(res.result == 1 && res.data != undefined && res.data != ""){
					processEnd("设置成功");
					getFamilyNumbers(imei);
				}
			},null,null);	
		}
	}
	function addSosType(imei){
		var sostype2 = "4";
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var sostype1 = $("#aterminal-detailform").contents().find("input[name='sostype1']:checked").val();		
		if(sostype1 != 4 || sostype1 != "4"){
			sostype2 = $("#aterminal-detailform").contents().find("input[name='sostype2']:checked").val();
		}
		var data = {
				cmd:"updateCard",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					sostype:sostype2,
					schoolcode:schoolcode
				},
				remark:"",
		};
		processBegin("正在应用中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("设置成功");
				
			}else{
				processEnd("设置失败");
			}
		},null,null);
	}
	function addSosList(imei){
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var list = [];
		for(var i = 0; i < 3; i ++){
//			if($("#aterminal-detailform").contents().find("#sos-number-" + (i+1)).val() != "" || $("#aterminal-detailform").contents().find("#sos-name-" + (i+1)).val() != ""){
				var obj = {};
				obj.imei = imei;
				obj.sosphone = $("#aterminal-detailform").contents().find("#sos-number-" + (i+1)).val();
				obj.remark = encodeURI($("#aterminal-detailform").contents().find("#sos-name-" + (i+1)).val());
				obj.sostype = 1;
				obj.schoolcode = schoolcode;
				for(var key in obj){
					if(obj[key] == undefined){
						obj[key] = "";
					}
				}
				list.push(obj);
//			}
		}
		if(list.length > 0){
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
					cmd:"addSosphones",
					model:"card",
					userKey:userKey,
					data:JSON.stringify(list),
					remark:"",
					
			};
			processBegin("正在应用中···");
			AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
				if(res.result == 1 && res.data != undefined && res.data != ""){
					processEnd("设置成功");
					getSosNumbers(imei);
				}
			},null,null);	
		}
	}
	function addWhiteList(imei,studentcode){
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var list = [];
		for(var i = 0; i < 15; i ++){
			if($("#aterminal-detailform").contents().find("#white-number-" + (i+1)).val() != "" || $("#aterminal-detailform").contents().find("#white-name-" + (i+1)).val() != ""){
				var obj = {};
				obj.imei = imei;
				obj.studentcode = studentcode;
				obj.incardcode = i+1;
				obj.incellphone = $("#aterminal-detailform").contents().find("#white-number-" + (i+1)).val();
				obj.remark = encodeURI($("#aterminal-detailform").contents().find("#white-name-" + (i+1)).val());
				obj.schoolcode = schoolcode;
				list.push(obj);
			}else{
				if(i == 0){
					var obj1 = {};
					obj1.imei = imei;
					obj1.studentcode = studentcode;
					obj1.incardcode = i+1;
					obj1.incellphone = $("#aterminal-detailform").contents().find("#white-number-" + (i+1)).val();
					obj1.remark = encodeURI($("#aterminal-detailform").contents().find("#white-name-" + (i+1)).val());
					obj1.schoolcode = schoolcode;
					list.push(obj1);
				}else{
					
				}
			}
		}
//		if(list.length > 0){
			var userKey = $("#user-name",parent.parent.document).attr("key");
			var data = {
					cmd:"addPhonebookTmps",
					model:"card",
					userKey:userKey,
					data:JSON.stringify(list),
					remark:"",
			};
			debugger;
			processBegin("正在应用中···");
			AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
				if(res.result == 1 && res.data != undefined && res.data != ""){
					processEnd("设置成功");
					getWhiteList(imei);
				}
			},null,null);	
//		}
	}
	
	function addWorktimelist(imei,work){
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var obj = {};
		if(work == 1){
			for(var i = 1; i < 25; i ++){
				if($("#aterminal-detailform").contents().find("#worktime" + i + "-1").val() != "" || $("#aterminal-detailform").contents().find("#worktime" + i + "-2").val() != ""){
					obj["start"+i] = $("#aterminal-detailform").contents().find("#worktime" + i + "-1").val();
					obj["stop"+i] = $("#aterminal-detailform").contents().find("#worktime" + i + "-2").val();					
				}else{
					obj["start"+i] = "";
					obj["stop"+i] = "";					
				}
			}
			
		}else if(work == 0){
			for(var j = 1; j < 25; j ++){
				if($("#aterminal-detailform").contents().find("#stealthtime" + j + "-1").val() != "" || $("#aterminal-detailform").contents().find("#stealthtime" + j + "-2").val() != ""){
					obj["start"+j] = $("#aterminal-detailform").contents().find("#stealthtime" + j + "-1").val();
					obj["stop"+j] = $("#aterminal-detailform").contents().find("#stealthtime" + j + "-2").val();					
				}else{
					obj["start"+j] = "";
					obj["stop"+j] = "";					
				}
			}
		}
		obj.cardcode = imei;
		obj.work = work;
		obj.schoolcode = schoolcode;
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var data = {
				cmd:"setCardWork",
				model:"card",
				userKey:userKey,
				data:obj,
				remark:"",
		};
		processBegin("正在应用中···");
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("设置成功");
				
			}else{
				processError("设置失败");
			}
		},null,null);	
		
	}
	
	function addSelectedRing(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var ring = $("#aterminal-detailform").contents().find("#table-ring input[name=\"terminalsetup_name\"]:checked").val();
		var ringurl = $("#aterminal-detailform").contents().find("#table-ring input[name=\"terminalsetup_name\"]:checked").attr("ringurl");
		var data = {
				cmd:"updateCard",
				model:"card",
				userKey:userKey,
				data:{
					imei:imei,
					ring:ring,
					ringurl:ringurl,
					schoolcode:schoolcode
				},
				remark:"",
		};
		processBegin("正在应用中···");
		AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("设置成功");
			}else{
				processError("设置失败");
			}
		},null,null);	
	}
	function terminalLoss(imei){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getTerminalLoss",
				model:"terminal",
				userKey:userKey,
				data:{
					asn:imei,
					schoolcode:schoolcode
				},
				remark:""
		};
		processBegin("正在挂失中···");
		AjaxCrud.get(serverUrl.TerminalLossServerLoginUrl,data,function(res){				
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("挂失成功");
				initTerminals(currentPage,pageSize);
			}else{
				processError("挂失失败");
			}
		});
	}
	
	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	function isUndefined(param){
		if(param == undefined){
			param = "";
		}
		return param;
	}
	function pagebtns(){
		$("#aterminal-name").unbind("keyup");
		$("#aterminal-name").keyup(function(){
			this.value=this.value.replace(/^ +| +$/g,'');
		});
		document.onkeydown = function(e){ 
		    var ev = document.all ? window.event : e;
		    if(ev.keyCode==13) {
		    	now = 0;
		    	initTerminals(currentPage,pageSize);
		    }
		};
		$("#aterminal-search-btn").click(function(){
			dataCache = [];
			now = 0;
			var islock = $("#state-aterminal").val();
			if(islock=="normal"){
				initTerminals(currentPage,pageSize);
			}else if(islock=="lock"){
				initLoss(currentPage,pageSize);
			}
		});
	}
	
	function initLoss(currentpage,pagesize){
		var userKey = $("#user-name",parent.parent.document).attr("key");	
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getLossList",
				model:"terminal",
				userKey:userKey,
				data:{
					currentpage:currentpage,
					pagesize:pagesize,
					schoolcode:schoolcode
				},
				remark:"",
		};
		AjaxCrud.get(serverUrl.TerminalLossServerLoginUrl,data,function(res){
			var content = "";
			var pagecontent = "<div class=\"pages-btn-next\">&gt;&gt;</div>";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.list;
				var total = res.data.total;
				totalSize = Math.ceil(total/pagesize);
				if(totalSize <= 5 && totalSize >0){
					for(var i = totalSize-1 ; i >= 0; i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}else if(totalSize == 0){
					pagecontent += "<div class=\"pages-btn\">1</div>";
				}else if(totalSize > 5){
					var lastp = (now*5)+5;
					if(lastp > totalSize){
						lastp = totalSize;
					}
					for(var i = lastp-1 ; i >= (now*5); i--){
						if(i == currentpage-1){
							pagecontent += "<div class=\"pages-btn page-btn-select\">"+(i + 1)+"</div>"; 
						}else{
							pagecontent += "<div class=\"pages-btn\">"+(i + 1)+"</div>"; 
						}
					}
				}
				pagecontent += "<div class=\"pages-btn-last\">&lt;&lt;</div>";
				$("#aterminal-page").empty();
				$("#aterminal-page").append(pagecontent);
				
				$(".pages-btn").unbind("click");
				$(".pages-btn").click(function(){
					dataCache = [];
					$(".pages-btn").removeClass("page-btn-select");
					$(this).addClass("page-btn-select");
					currentPage = parseInt($(this).text());
					initLoss(currentPage,pageSize);
				});
				
				$(".pages-btn-last").unbind("click");
				$(".pages-btn-last").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(now > 0){
							now --;
							initLoss((now*5+1),pageSize);
						}else{
							return false;
						}
					}
				});
				
				$(".pages-btn-next").unbind("click");
				$(".pages-btn-next").click(function(){
					if(totalSize <= 5 && totalSize >0){
						return false;
					}else if(totalSize == 0){
						return false;
					}else if(totalSize > 5){
						if(totalSize%5 == 0){
							if((now+1) < totalSize/5){
								now ++;
								initLoss((now*5+1),pageSize);
							}else{
								return false;
							}
						}else{
							if((now+1) <= (totalSize-totalSize%5)/5){
								now ++;
								initLoss((now*5+1),pageSize);
							}else{
								return false;
							}
						}
						
					}
				});
				
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" ){
								data[i][key] = "";
							}
						}
//						var lockBtns = "";
//						if(data[i].islock == true || data[i].islock == "true"){
//							lockBtns = "<div class=\"table-locked\" usercode = '"+data[i].usercode+"' id=\"lockuser\" title=\"锁定\"></div>";
//						}else if(data[i].islock == false || data[i].islock == "false"){
//							lockBtns = "<div class=\"table-unlocked\" usercode = '"+data[i].usercode+"' id=\"unlockuser\" title=\"解锁\"></div>";
//						}
						content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\">" +
							"<div class=\"tables-left\">" +
								"<div class=\"table-name\" title=\""+"\"\>"+"卡号"+"</div>" +
								"<div class=\"table-code\" style=\"width:140px;\" title=\"卡号\">"+data[i].outid+"</div>" +
							"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
							"<div class=\"table-address\" title=\"\">"+data[i].updateflag+"</div>" +
								"<div class=\"table-date\" title=\"挂失时间\">"+data[i].updatedt+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
								"<div class=\"table-oprate\">" +
									/*"<div class=\"table-detail\" imei = \""+data[i].imei+"\" studentcode = \""+data[i].studentcode+"\" title=\"详情\"></div>" +
									"<div class=\"table-edit\" imei = \""+data[i].imei+"\" studentcode = \""+data[i].studentcode+"\" title=\"设置\"></div>" +
									"<div class=\"table-locked\" imei = \""+data[i].imei+"\"  title=\"挂失\"></div>" +
									//lockBtns +
*/								"</div>" +
							"</div>" +
						"</div>";
						dataCache.push(data[i]);
					}
					$("#aterminal-data-show").empty();
					$("#aterminal-data-show").append(content);
					
					$(".table-trs").unbind("hover");
					$(".table-trs").hover(function(){
						$(this).find(".table-oprate").show();
					},function(){
						$(".table-oprate").hide();
					});
					
					
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#aterminal-data-show").empty();
					$("#aterminal-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#aterminal-data-show").empty();
				$("#aterminal-data-show").append(content);
			}
		},null,null);	
	}
	
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();